package com.ruoyi.vehicle.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.vehicle.domain.TbRoute;
import com.ruoyi.vehicle.domain.TbRouteStation;
import com.ruoyi.vehicle.service.ITbRouteService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.vehicle.mapper.TbRouteStationMapper;

/**
 * 线路基本信息Controller
 * 
 * @author ruoyi
 * @date 2025-10-25
 */
@RestController
@RequestMapping("/route")
public class TbRouteController extends BaseController
{
    @Autowired
    private ITbRouteService tbRouteService;


    @Autowired
    private TbRouteStationMapper tbRouteStationMapper;

    /**
     * 查询线路基本信息列表
     */
    ////@RequiresPermissions("vehicle:route:list")
    @GetMapping("/list")
    public TableDataInfo list(TbRoute tbRoute)
    {
        startPage();
        List<TbRoute> list = tbRouteService.selectTbRouteList(tbRoute);
        return getDataTable(list);
    }

    /**
     * 导出线路基本信息列表
     */
    ////@RequiresPermissions("vehicle:route:export")
    @Log(title = "线路基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbRoute tbRoute)
    {
        List<TbRoute> list = tbRouteService.selectTbRouteList(tbRoute);
        ExcelUtil<TbRoute> util = new ExcelUtil<TbRoute>(TbRoute.class);
        util.exportExcel(response, list, "线路基本信息数据");
    }

    /**
     * 获取线路基本信息详细信息
     */
    ////@RequiresPermissions("vehicle:route:query")
    @GetMapping(value = "/{routeId}")
    public AjaxResult getInfo(@PathVariable("routeId") Long routeId)
    {
        // 获取线路信息
        TbRoute route = tbRouteService.selectTbRouteByRouteId(routeId);
        // 获取站点信息
        List<TbRouteStation> stations = tbRouteStationMapper.selectTbRouteStationByRouteId(routeId);
        AjaxResult result = success(route);
        result.put("stations", stations);
        return result;
    }

    /**
     * 新增线路基本信息
     */
    ////@RequiresPermissions("vehicle:route:add")
    @Log(title = "线路基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbRoute tbRoute)
    {
        return toAjax(tbRouteService.insertTbRoute(tbRoute));
    }

    /**
     * 修改线路基本信息
     */
    ////@RequiresPermissions("vehicle:route:edit")
    @Log(title = "线路基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbRoute tbRoute)
    {
        return toAjax(tbRouteService.updateTbRoute(tbRoute));
    }

    /**
     * 保存线路及站点信息
     */
    ////@RequiresPermissions("vehicle:route:edit")
    @Log(title = "线路基本信息", businessType = BusinessType.UPDATE)
    @PostMapping("/saveRouteAndStations")
    public AjaxResult saveRouteAndStations(@RequestBody RouteWithStations routeWithStations)
    {
        return toAjax(tbRouteService.saveRouteAndStations(routeWithStations.getRoute(), routeWithStations.getStations()));
    }

    /**
     * 删除线路基本信息
     */
    ////@RequiresPermissions("vehicle:route:remove")
    @Log(title = "线路基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{routeIds}")
    public AjaxResult remove(@PathVariable Long[] routeIds)
    {
        return toAjax(tbRouteService.deleteTbRouteByRouteIds(routeIds));
    }

    /**
     * 内部静态类，用于接收线路和站点信息
     */
    static class RouteWithStations
    {
        private TbRoute route;
        private List<TbRouteStation> stations;

        public TbRoute getRoute()
        {
            return route;
        }

        public void setRoute(TbRoute route)
        {
            this.route = route;
        }

        public List<TbRouteStation> getStations()
        {
            return stations;
        }

        public void setStations(List<TbRouteStation> stations)
        {
            this.stations = stations;
        }
    }
}